/*
 * @Description:
 * @Author: dh
 * @Date: 2022-01-24 14:48:57
 * @LastEditors: dh
 * @LastEditTime: 2022-01-24 14:54:09
 */
$.fn.extend({
	myfullpage: function (dataObj) {
		this.each(function (index, ele) {
			var _this = $(ele)
			dataObj.time = dataObj.time || 1000
			var outerHeight = $(window).outerHeight(),
				outerWidth = $(window).outerWidth()
			$('html,body').css({
				position: 'relative',
				overflow: 'hidden',
				width: '100%',
				height: '100%',
				margin: 0,
			})
			var section = _this
				.css({
					position: 'absolute',
					top: 0,
					left: 0,
					width: '100%',
					height: '100%',
				})
				.find('.section')
				.css({
					position: 'relative',
					width: '100%',
					height: '100%',
				})

			section.each(function (index, ele) {
				$(ele).css({
					backgroundColor: dataObj.colorArray[index],
				})
				var len = $(ele).find('.slide').css({
					position: 'relative',
					width: outerWidth,
					height: outerHeight,
					float: 'left',
				}).length
				if (len) {
					var slideAll = $(ele)
						.find('.slide')
						.wrapAll('<div class="slideAll">')
						.closest('.slideAll')
						.css({
							position: 'absolute',
							top: 0,
							left: 0,
							width: outerWidth * len,
							htight: outerHeight,
						})
				}
			})
			section.eq(0).addClass('active')
			section.each(function (index, ele) {
				$(ele).find('.slide').eq(0).addClass('innerActive')
			})
			var curIndex = 0,
				lock = true,
				direction = null
			$(document).on('keydown', function (e) {
				// left == 37  top == 38  right == 39  bottom == 40
				if (lock) {
					if (e.keyCode == 38 || e.keyCode == 40) {
						var newTop = _this.offset().top
						// top/bottom
						if (e.keyCode == 38 && curIndex != 0) {
							direction = 'top'
							typeof dataObj.onLeave === 'function' && dataObj.onLeave(curIndex, direction)
							curIndex--
							newTop += outerHeight
						} else if (e.keyCode == 40 && curIndex != section.length - 1) {
							direction = 'bottom'
							typeof dataObj.onLeave === 'function' && dataObj.onLeave(curIndex, direction)
							curIndex++
							newTop -= outerHeight
						} else {
							lock = true
							return
						}
						lock = false
						_this.animate(
							{
								top: newTop + 'px',
							},
							dataObj.time,
							'easeInBack',
							function () {
								_this.css({
									top: newTop + 'px',
								})
								section.removeClass('active').eq(curIndex).addClass('active')
								lock = true
								typeof dataObj.afterLoad === 'function' && dataObj.afterLoad(curIndex, direction)
							}
						)
					}

					// var innerActiveIndex = innerActive.index();
					if (e.keyCode == 37 || e.keyCode == 39) {
						var slideAll = _this.find('.active .slideAll'),
							slide = _this.find('.active .slide'),
							inActIndex = _this.find('.active .innerActive').index()
						if (inActIndex == -1) {
							return
						}
						// left/slide
						var newLeft = slideAll.offset().left
						if (e.keyCode == 37 && inActIndex > 0) {
							direction = 'left'
							typeof dataObj.onLeave === 'function' && dataObj.onLeave(inActIndex, direction)
							inActIndex--
							newLeft += outerWidth
						} else if (e.keyCode == 39 && inActIndex != slide.length - 1) {
							direction = 'right'
							typeof dataObj.onLeave === 'function' && dataObj.onLeave(inActIndex, direction)
							inActIndex++
							newLeft -= outerWidth
						} else {
							lock = true
							return
						}
						lock = false
						slideAll.animate(
							{
								left: newLeft + 'px',
							},
							dataObj.time,
							'easeInBack',
							function () {
								slideAll.css({
									left: newLeft + 'px',
								})
								slide.removeClass('innerActive').eq(inActIndex).addClass('innerActive')
								lock = true
								typeof dataObj.afterLoad === 'function' && dataObj.afterLoad(inActIndex, direction)
							}
						)
					}
				}
			})
			_this.fadeIn(dataObj.time)
		})
		return this
	},
	// 该自定义插件可以根据数组、对象构建/配置指定元素标签、
	createDom: function (data) {
		if (typeof data != 'object') {
			console.log('类型错误！！createDom(data) data 可以传两种类型，[]、{}')
			return this
		}
		// data 可以传两种类型，[]、{}
		data = $.type(data) == 'array' ? data : [data]
		this.each(function (index, _this) {
			var _this = $(_this)
			var $div = null
			var reg = /</g
			data.forEach(function (ele, index) {
				// 判断 创建 / 选择已有元素、
				$div = reg.test(ele.type) ? $(ele.type) : $(ele.type, _this)
				ele.text && $div.text(ele.text)
				ele.class && $div.addClass(ele.class)
				ele.css && $div.css(ele.css)
				ele.event && $div.on(ele.event)
				// 回调函数提供接口调用能力、
				typeof ele.adjustFn === 'function' && ele.adjustFn($div)
				if (ele.dom) {
					ele.dom = $.type(ele.dom) == 'array' ? ele.dom : [ele.dom]
					ele.dom.forEach(function (e, i) {
						$div.createDom(e)
					})
				}
				_this.append($div)
			})
		})
		return this
	},
})
